<template>
  <div>
    <div class="table-page-search-wrapper">
      <a-form-model
        ref="formModel"
        :model="form"
        layout="inline"
        :rules="{
          name: [
            {
              validator: validators.noWhitespace(),
              trigger: 'change',
            },
            {
              validator: validators.noIllegalString(),
              trigger: 'change',
            },
            {
              max: 10,
              message: '长度在 1 到 10 个字符',
              trigger: 'change',
            },
          ],
        }"
      >
        <a-row :gutter="[16, 16]" type="flex" justify="end">
          <a-col :md="6" :sm="8">
            <a-form-model-item prop="name" label="应用名称">
              <a-input autocomplete="off" v-model="form.name" />
            </a-form-model-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-model-item label="数据中心">
              <a-select
                :getPopupContainer="
                triggerNode => {
                  return triggerNode.parentNode || document.body;
                }"
                v-model="form.datacenterId">
                <a-select-option
                  v-for="d in datacenterList"
                  :key="d.id"
                  :value="d.id"
                >
                  {{ d.name }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <span
            style="float: left; overflow: hidden"
            class="table-page-search-submitButtons"
          >
            <a-col :md="12" :sm="24">
              <a-button type="primary" @click="loadData(true)" icon="search">
                查询
              </a-button>
              <a-button
                @click="resetSearchForm"
                icon="reload"
                style="margin-left: 8px"
              >
                重置
              </a-button>
            </a-col>
          </span>
        </a-row>
      </a-form-model>
    </div>

    <a-table
      ref="table"
      rowKey="id"
      size="middle"
      :columns="columns"
      :dataSource="gDataSource"
      :pagination="gPagination"
      @change="gHandleTableChange"
    >
      <span slot="sortCol" slot-scope="text, record, index">
        {{
          (gPagination.current - 1) * gPagination.pageSize + parseInt(index) + 1
        }}
      </span>
      <span slot="instance" slot-scope="text, record">
        <template v-if="record.cloudResourceTypeName === '容器云'">
          <a-icon type="cloud" />
        </template>
        <template v-else>
          <a-icon type="desktop" />
        </template>
        {{ text }}
      </span>
      <span slot="cloudType" slot-scope="text, record" style="vertical-align: middle;">
        <template v-if="record.cloudResourceTypeName === '容器云'">
          <img src="~@/assets/k8s_logo.svg" alt="logo" style="width: 18px">
        </template>
        <template v-else-if="record.cloudResourceTypeName === '华为云'">
          <img src="~@/assets/huawei_logo.png" alt="logo" style="width: 18px">
        </template>
        <template v-else>
          <img src="~@/assets/aliyun_logo.png" alt="logo" style="width: 18px">
        </template>
        {{ text }}
      </span>
    </a-table>
  </div>
</template>

<script>
import { GwListMixin } from "@/mixins/GwListMixin";
import { gPostAction } from "@/api/manage";

export default {
  name: "SchedulerAlarm",
  mixins: [GwListMixin],
  data() {
    return {
      // 表头
      columns: [
        {
          title: "序号",
          dataIndex: "",
          key: "rowIndex",
          scopedSlots: { customRender: "sortCol" },
        },
        {
          title: "调度应用名称",
          dataIndex: "appName",
        },
        {
          title: "调度实例",
          dataIndex: "instanceName",
          scopedSlots: { customRender: "instance" },
        },
        {
          title: "云平台资源",
          dataIndex: "cloudResourceName",
        },
        {
          title: "调度类型",
          dataIndex: "cloudResourceTypeName",
          scopedSlots: { customRender: "cloudType" },
        },
        {
          title: "原数据中心",
          dataIndex: "srcDatacenterName",
        },
        {
          title: "目标数据中心",
          dataIndex: "dstDatacenterName",
        },
        {
          title: "异常信息",
          dataIndex: "errorStep",
          customRender: (t, r, index) => t + "失败",
        },
        {
          title: "出现时间",
          dataIndex: "endTime",
        },
      ],
      url: {
        list: "/scheduler/job/page",
      },
      gDataSource: [],
      form: {
        name: "",
        datacenterId: "",
      },
      datacenterList: [],
    };
  },
  mounted() {
    this.loadDatacenter();
    this.loadData();
  },
  methods: {
    loadData(isDoSearch) {
      this.$refs.formModel.validate((error) => {
        if (!error) {
          return;
        }
        this.gQueryParam.name = this.form.name;
        this.gQueryParam.title = "多数据中心调度-动态调度机制-调度结果审计-调度异常告警";
        this.gQueryParam.content = "查询调度异常告警列表";
        this.gQueryParam.datacenterId = this.form.datacenterId;
        this.gQueryParam.status = 2;
        if (isDoSearch) {
          this.gQueryParam.pageNo = 1;
          this.gPagination.current = 1;
        }
        gPostAction(this.url.list, this.gQueryParam).then((res) => {
          const data = res.data;
          if (data && data.list) {
            this.gDataSource = data.list;
            this.gPagination.total = data.totalCount;
          }
        });
      });
    },
    loadDatacenter() {
      gPostAction("/asset/pageDatacenter", {}).then((res) => {
        if (res.success) {
          this.datacenterList = res.data.list;
          this.datacenterList.unshift({id:'',name:'全部'})
        }
      });
    },
    handleDetail(data) {
      this.$refs.detailModal.showModal();
    },
    handleConfig(data) {
      this.$refs.configModal.config(data);
    },
    resetSearchForm() {
      this.form.name = "";
      this.form.datacenterId = "";
      this.$refs.formModel.resetFields();
    },
  },
};
</script>
